/**
 * 
 */
package controllers;

import java.util.*;

import com.fasterxml.jackson.databind.node.ObjectNode;

import models.Table;
import play.*;
import play.libs.Json;
import play.mvc.*;
import views.html.*;
import models.Table;
import models.Client;

/**
 * @author Administrator
 *
 */

@SuppressWarnings("unused")
public class Clients extends Controller {
	
	final static int ROOM_FULL = -2;
	final static int USER_EXISTS = -1;
	final static int USER_NOT_EXISTS = -3;
	
	// lista podlaczonych klientow
	public static Client ClientsArray[] = null;
	// id hosta
	public static int HostID = -1;
	
	
	public static int GetUserID(String User) {
		if ( ClientsArray == null ) 
    		return -4;
    	
    	for ( int i=0; i<4; i++ ) {
    		if ( ClientsArray[i] == null ) continue;
    		
    		if ( ClientsArray[i].getUser().equals(User) )
    			return i;
    	}
    	
    	return USER_NOT_EXISTS; // exception ?
    }
    
	public static int GetClientsNumber() { 
    	
    	int size = 0;
    	
    	for ( int i=0; i<4; i++ ) {
    		if ( ClientsArray[i] != null )
    			size++;
    	}
    	
    	return size;
    }
	
	public static int GetTotalClientsNumber() {
		return ClientsArray.length;
	}
	
	
	public static Client GetClientByID(int ID) {
		if ( ID < 0 ) return null;
		return ClientsArray[ID];
	}
	
	public static Client GetClientByName(String User) {

		for ( int i=0; i<4; i++ ) {
    		if ( ClientsArray[i] != null )
    			if ( ClientsArray[i].getUser().equals(User) )	return ClientsArray[i];
    	}
		
		return null;
	}
	
	public static boolean IsHost(int ID) {
		if ( HostID == ID ) return true;
		return false;
	}
    
	public static int NewClientConnected(String User) {
    	
    	//System.out.println(remote);
    	
    	if ( ClientsArray == null ) {
    		ClientsArray = new Client[4];
    		
    		for ( int i=0; i<4; i++ )
    			ClientsArray[i] = null;
    	}
    	else
    	{
    		// check if username exists
    		for ( int i=0; i<4; i++ ) {
    			if ( ClientsArray[i] == null ) continue;
    			
        		if ( ClientsArray[i].getUser().equals(User) )
        			return USER_EXISTS; // throw exception or sth
        	}
    	}
    	
    	// get id for the user
    	for ( int i=0; i<4; i++ ) {
    		if ( ClientsArray[i] == null ) {
    			ClientsArray[i] = new Client(User,i);
    			return i;
    		}
    	}
    	
    	return ROOM_FULL;
    }
    
	public static void ClientDisconnect(int ID) {
    	if ( ClientsArray == null ) 
    		return;

    	if ( ClientsArray[ID] == null ) 
    		return;
    	
    	try {
    		Game.GameLeft(ClientsArray[ID]);
    		ClientsArray[ID] = null;
    	}
    	catch ( Exception ex ) {
    		// ...
    		ex.printStackTrace();
    		return;
    	}
    		
    	if ( ID == HostID )	{
    		
    		HostID = -1;
    		
    		for ( int i=0; i<4; i++ ) {
        		if ( ClientsArray[i] != null && i != ID )
        			HostID = i;
        	}
        }
    }
	
	public static void SendMessageToAll(String FieldName, String FieldName2, String Message) {
		
		ObjectNode event = Json.newObject();
        event.put(FieldName2, Message);
        ObjectNode event2 = Json.newObject();
        event2.put(FieldName, event);
        
        Client C = null;

    	// send message to ClientsArray
    	for ( int i=0; i<GetTotalClientsNumber(); i++ ) {
    		
    		C = Clients.GetClientByID(i);
    		if ( C == null || C.getOut() == null ) continue;
    		
    		C.getOut().write(event2);
    	}
	}
	
	public static void SendMessageToAll(ObjectNode e) {
		
        Client C = null;

    	// send message to ClientsArray
    	for ( int i=0; i<GetTotalClientsNumber(); i++ ) {
    		
    		C = Clients.GetClientByID(i);
    		if ( C == null || C.getOut() == null ) continue;
    		
    		C.getOut().write(e);
    	}
	}
	
	public static void SendMessageToAll(String FieldName, String Message) {
		
		ObjectNode event = Json.newObject();
        event.put(FieldName, Message);
        
        Client C = null;

    	// send message to ClientsArray
    	for ( int i=0; i<GetTotalClientsNumber(); i++ ) {
    		
    		C = Clients.GetClientByID(i);
    		System.out.println("  Client ID"+i+": "+C);
    		if ( C == null || C.getOut() == null ) continue;
    		
    		System.out.println("     Message: {"+FieldName+":"+Message+"}     Sending to: "+C.getUser());
    		C.getOut().write(event);
    	}
	}
	
	public static void SendMessageToAll(String FieldName, int Value) {
		
		ObjectNode event = Json.newObject();
        event.put(FieldName, Value);
        
        Client C = null;

    	// send message to ClientsArray
    	for ( int i=0; i<GetTotalClientsNumber(); i++ ) {
    		
    		C = Clients.GetClientByID(i);
    		//System.out.println("  Client ID"+i+": "+C);
    		if ( C == null || C.getOut() == null ) continue;
    		
    		//System.out.println("     Message: {"+FieldName+":"+Value+"}     Sending to: "+C.getUser());
    		C.getOut().write(event);
    	}
	}
	
	public static void SendMessageTo(Client client,String FieldName, String FieldName2, String Message) {
		
		if ( client == null ) return;
		
		ObjectNode event = Json.newObject();
        event.put(FieldName2, Message);
        ObjectNode event2 = Json.newObject();
        event2.put(FieldName, event);

        client.getOut().write(event2);
	}
    
	public static void SendMessageTo(Client client, String FieldName, String Message) {
		
		if ( client == null ) return;
		
		ObjectNode event = Json.newObject();
        event.put(FieldName, Message);

        client.getOut().write(event);
	}
	
	public static void SendMessageTo(Client client, String FieldName, int Value) {
		
		if ( client == null ) return;
		
		ObjectNode event = Json.newObject();
        event.put(FieldName, Value);
 
        System.out.println("     Message: {"+FieldName+":"+Value+"}     Sending to: "+client.getUser());
        client.getOut().write(event);
	}
	
	public static void SendMessageTo(Client client, ObjectNode e) {
		
		if ( client == null ) return;
		
		client.getOut().write(e);
	}
	
	public static String GetErrorMessage( int ErrorID ) {
    	
    	if ( ErrorID == ROOM_FULL ) return "Game room is full!";
    	if ( ErrorID == USER_EXISTS ) return "User already logged in!";
    	if ( ErrorID == USER_NOT_EXISTS ) return "Woops, not existing user!";
    	
    	return "Unknown error!";
    	
    }

}
